package com.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Statement;
import com.web.dao.ReceiveAddressDAO;
import com.web.model.ReceiveAddress;
import com.web.util.DBConnection;

public class ReceiveAddressDAOImp implements ReceiveAddressDAO{
	private final String FIND_BY_ID="select * from d_receive_address where id = ?";
	
	private static final String INSERT="insert into d_receive_address"
			+"(id,user_id,receive_name,full_address,postal_code,mobile,phone)"
			+"values(?,?,?,?,?,?,?)";
	
	private final String FIND_BY_USER_ID="select * from d_receive_address where user_id = ?";
	
	public ReceiveAddress findById(int id) throws Exception{
		PreparedStatement prep= null;
		Connection conn= null;
		ReceiveAddress receiveAddress= null;
		ResultSet rst= null;
		try{
			conn= DBConnection.getConnection();		    	  
			prep= conn.prepareStatement(FIND_BY_ID);
			prep.setInt(1, id);
			rst= prep.executeQuery();
			if(rst.next()){
				receiveAddress=new ReceiveAddress();
				receiveAddress.setFullAddress(rst.getString("full_address"));
				receiveAddress.setId(rst.getInt("id"));
				receiveAddress.setMobile(rst.getString("mobile"));
				receiveAddress.setPhone(rst.getString("phone"));
				receiveAddress.setPostalCode(rst.getString("postal_code"));
				receiveAddress.setReceiveName(rst.getString("receive_name"));
				receiveAddress.setUserId(rst.getInt("user_id"));
			}
		}finally{
			DBConnection.close(prep, conn);
		}
		return receiveAddress;
	}
	
	public void save(ReceiveAddress addr) throws Exception{
		PreparedStatement prep= null;
		Connection conn= null;
		try{
			conn= DBConnection.getConnection();
			prep= conn.prepareStatement(INSERT,Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1,addr.getId());
			prep.setInt(2,addr.getUserId());
			prep.setString(3,addr.getReceiveName());
			prep.setString(4, addr.getFullAddress());
			prep.setString(5, addr.getPostalCode());
			prep.setString(6, addr.getMobile());
			prep.setString(7, addr.getPhone());
			prep.executeUpdate();
			ResultSet rst= prep.getGeneratedKeys();
			rst.next();
			int id= rst.getInt(1);
			addr.setId(id);
		}finally{
			DBConnection.close(prep, conn);
		} 
	}
	public List<ReceiveAddress> findByUserId(int userId) throws Exception{
		PreparedStatement prep= null;
		Connection conn= null;
		List<ReceiveAddress> addrs=new ArrayList<ReceiveAddress>();
		ReceiveAddress receiveAddress= null;
		ResultSet rst= null;
		try{
			conn= DBConnection.getConnection();		    	  
			prep= conn.prepareStatement(FIND_BY_USER_ID);
			prep.setInt(1,userId);
			rst= prep.executeQuery();
			if(rst.next()){
				receiveAddress.setFullAddress(rst.getString("full_address"));
				receiveAddress.setId(rst.getInt("id"));
				receiveAddress.setMobile(rst.getString("mobile"));
				receiveAddress.setPhone(rst.getString("phone"));
				receiveAddress.setPostalCode(rst.getString("postal_code"));
				receiveAddress.setReceiveName(rst.getString("receive_name"));
				receiveAddress.setUserId(rst.getInt("user_id"));
				
				addrs.add(receiveAddress);
			}
		}finally{
			DBConnection.close(prep, conn);
		}
		return addrs;
	}

}
